package com.example.apricots.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.apricots.ActivityLogin;
import com.example.apricots.ActivityUpdatePwd;
import com.example.apricots.MainActivity;
import com.example.apricots.R;
import com.example.apricots.Services.BGMusicService;
import com.example.apricots.bean.User;
import com.example.apricots.db.DBManager;

import java.lang.ref.WeakReference;

public class FragmentSetting extends Fragment implements View.OnClickListener {

    private static final String TAG = FragmentSetting.class.getSimpleName();
    TextView txt_language;
    TextView txt_login_out;
    TextView txt_unregist_account;
    TextView modify_pwd;
    TextView closejpService;
    View v;
    User user;
    private Myhandler mHandler;
    MainActivity activity;
    /**
     * ActivityMain上下文
     */
    public static Context context;

    /**
     * 异步处理
     */
    private static class Myhandler extends Handler{
        private WeakReference<FragmentSetting> reference;
        private Myhandler(FragmentSetting fragmentSetting){
            reference=new WeakReference<>(fragmentSetting);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            FragmentSetting context=reference.get();
            if(context != null){
                switch (msg.what){
                    //
                }
            }
        }
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_setting, container, false);
        initview();
        return v;
    }

    private void initview() {
        activity= (MainActivity) getActivity();
        txt_language = v.findViewById(R.id.language);
        txt_language.setOnClickListener(this);
        txt_login_out=v.findViewById(R.id.login_out);
        txt_login_out.setOnClickListener(this);
        txt_unregist_account=v.findViewById(R.id.unregist_account);
        txt_unregist_account.setOnClickListener(this);
        modify_pwd=v.findViewById(R.id.modify_pwd);
        modify_pwd.setOnClickListener(this);
        closejpService=v.findViewById(R.id.close_jingpai);
        closejpService.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.language:
                switchLanguage();
                break;
            case R.id.login_out:
                loginOut();
                break;
            case R.id.unregist_account:
                unRegistAccount();
                break;
            case R.id.modify_pwd:
                modifyPwd();
                break;
            case R.id.close_jingpai:
                if(activity != null){
                    activity.releaseService();
                    activity.stopService(new Intent(getActivity(), BGMusicService.class));
                    Log.e(TAG, "onClick: jingpai is closed" );
                }else {
                    Log.e(TAG, "onClick: activity is null" );
                }

                break;
        }
    }

    /**
     * 修改密码
     */
    private void modifyPwd() {
        startActivity(new Intent(getActivity(), ActivityUpdatePwd.class));
    }

    /**
     * 注销账号
     */
    private void unRegistAccount() {
        user = (User) this.getArguments().getSerializable("user");
        if(user != null){
            //通过userid删除用户
            DBManager.getInstance().deleteUser(user);
            Toast.makeText(context,"注销成功",Toast.LENGTH_SHORT).show();

        }
    }

    /**
     * 退出登录
     */
    private void loginOut() {
        Toast.makeText(context,"退出登录",Toast.LENGTH_SHORT).show();
        startActivity(new Intent(getActivity(), ActivityLogin.class));
    }

    /**
     * 选择语言
     */
    private void switchLanguage() {
        Toast.makeText(context,"选择语言",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
    }

    /**
     * 发送handler消息
     */
    private void sendHandMessage(int what,long dealy){
        if(mHandler != null && !mHandler.hasMessages(what)){
            mHandler.sendEmptyMessageDelayed(what,dealy);
        }
    }
}
